<?php

/**
 * File Description
 * 
 * Kao: PHP Web Framework <http://www.byhuy.com/kao/>.
 * Copyright 2007-2008, Huy Nguyen <http://www.byhuy.com/>.
 * 
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 * 
 */

class Kao_App_Request
{
    
    /**
     * The HTTP method of this request
     * 
     * Kao_Http::GET | POST | PUT | DELETE | HEAD
     */
    protected $_httpMethod;
    
    /**
     * Request URI
     */
    protected $_requestUri;
    
    /**
     * Request URL
     */
    protected $_requestUrl;
    
    /**
     * Base URL for the request
     */
    protected $_baseUrl;
 
    
    /**
     * Controller
     * 
     * The name of the requested controller
     * 
     * @var string
     */
    protected $_controllerName = null;

    
    public function __construct($httpMethod, $requestUri, $requestUrl, $baseUrl, $controllerName)
    {
        $this->_httpMethod = $httpMethod;
        $this->_requestUri = $requestUri;
        $this->_requestUrl = $requestUrl;
        $this->_baseUrl = $baseUrl;
        $this->_controllerName = $controllerName;
    }
    
    /**
     * Get value of a member of $_SERVER or get from apache_request_headers().
     * 
     * Learned from Zend Framework ( Zend_Controller_Request_Http)
     */
    
    public function getHeader($header)
    {
        if (empty($header))
        {
            return false;
        }
        
        $phpServerKey = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
        
        if (isset($_SERVER[$phpServerKey]))
        {
            return $_SERVER[$phpServerKey];
        }
        
        if (function_exists('apache_request_headers')) 
        {
            $headers = apache_request_headers();
            
            if (!empty($headers[$header])) 
            {
                return $headers[$header];
            }
        }
        
        return false;
    }
    
    
    /**
     * Set controller name.
     * 
     * Called when routing.
     * 
     * @param string $controllerName The Name of the controller
     * @return Kao_App_Request
     */
    public function setControllerName($controllerName)
    {
        $this->_controllerName = $controllerName;
        return $this;
    }
    
    /**
     * Return the controller name
     * 
     * @return string
     */
    public function getControllerName()
    {
        return $this->_controllerName;
    }
    
    /**
     * 
     */
    public function getParam($key = full, $default = null)
    {
       if (null === $key)
        {
            return $_GET;
        }
        return ((isset($_GET[$key])) ? $_GET[$key] : $default); 
    }
    
    /**
     * 
     */
    public function getPost($key = null, $default = null)
    {
        if (null === $key)
        {
            return $_POST;
        }
        return ((isset($_POST[$key])) ? $_POST[$key] : $default);
    }
    
    /**
     * 
     */
    public function getFile($key)
    {
        if (null === $key)
        {
            return $_FILES;
        }
        return ((isset($_FILES[$key])) ? $_FILES[$key] : false);
    }
    
    /**
     * 
     */
    public function getCookie($key = null, $default = null)
    {
        if (null === $key)
        {
            return $_COOKIE;
        }
        return ((isset($_COOKIE[$key])) ? $_COOKIE[$key] : $default);
    }
    
    /**
     * Get raw request body
     * 
     * @return string
     */
    public function getRawBody()
    {
        return file_get_contents('php://input');
    }
}


